home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 45 / Amiga Format CD45 (1999-09)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-11].iso / -serious- / wb / wreplaceicon / wreplaceicon.e < prev    next >
Text File  |  1999-08-16  |  11KB  |  335 lines

  1. -> WReplaceIcon V2.0 by Jilles Tjoelker 1999-07-15
  2. -> Requires V41 BGUI modules, though V40 library should work.
  3. -> 'libraries/bgui_macros' is an old module, so bgui.library V40
  4. -> may work. If you don't have it, you may substitute 'libraries/bguim'
  5. -> but it will not work 100% under V40.
  6. -> TAB=2
  7. ->
  8. -> New in V2.0:
  9. ->
  10. -> * About button replaced by About option in menu.
  11. -> * Options to copy or not image, type & default tool and tool types.
  12. -> * Sizing gadget removed as it does not have any use.
  13. -> * Reading of tool types added.
  14. -> * File requester to select icons added.
  15. ->
  16. -> Tool types (read from icon whether started from WB or CLI):
  17. ->
  18. -> EQUALWIDTH: make the two areas the same size (yes/no)
  19. -> MAXWIDTH,MAXHEIGHT: maximum size of images shown in the source box
  20. -> COPYIMAGE,COPYTYPE,COPYTOOLTYPES: same options as in the menu (yes/no)
  21. -> DEFDRAWER: default drawer for file requester (default: ENV:Sys)
  22.  
  23. OPT PREPROCESS,OSVERSION=37
  24.  
  25. MODULE 'bgui','libraries/bgui','libraries/bguic','libraries/bgui_macros',
  26.              'tools/boopsi','utility/tagitem','intuition/screens',
  27.              'intuition/gadgetclass','intuition/classusr','intuition/intuition',
  28.              'intuition/classes','workbench/workbench','icon',
  29.              'workbench/startup','libraries/gadtools','libraries/asl'
  30.  
  31. ENUM ID_NULL,ID_SRC,ID_ABOUT,ID_SELECT,ID_QUIT,ID_CIMAGE,ID_CTYPE,ID_CTTYPES
  32.  
  33. SET CP_IMAGE,CP_TYPE,CP_TTYPES
  34.  
  35. ENUM ERR_NONE,ERR_LIB,ERR_GUI,ERR_WIN
  36.  
  37. RAISE ERR_LIB IF OpenLibrary()=NIL
  38.  
  39. DEF wd_obj=NIL,sig,done,wnd,filereq=NIL
  40. DEF doeqw=FALSE,maxw=100,maxh=60,copy=CP_IMAGE,defdrawer[256]:STRING
  41. DEF g_Src,g_Dest
  42. DEF diskobj_src=NIL:PTR TO diskobject,
  43.         diskobj_dst=NIL:PTR TO diskobject
  44.  
  45. PROC bguireq(title,text,gadgets,args=NIL)
  46.     DEF rq:bguiRequest
  47.     rq.flags:=BREQF_AUTO_ASPECT OR BREQF_LOCKWINDOW OR
  48.         (IF InStr(gadgets,'*')<>-1 THEN BREQF_FAST_KEYS ELSE 0)
  49.     rq.title:=title
  50.     rq.gadgetFormat:=gadgets
  51.     rq.textFormat:=text
  52.     rq.reqPos:=POS_CENTERMOUSE
  53.     rq.textAttr:=NIL
  54.     rq.underscore:="_"
  55.     rq.reserved0:=0
  56.     rq.screen:=NIL -> NOP
  57.     rq.reserved1:=0
  58. ENDPROC BgUI_RequestA(wnd,rq,args)
  59.  
  60. INT 0
  61. verstag:
  62. CHAR '$VER: WReplaceIcon 2.0 (15.7.99)',0
  63.  
  64. PROC main() HANDLE
  65.     DEF amsg:PTR TO appmessage,wa:PTR TO wbarg,cd
  66.     DEF name[256]:STRING
  67.     DEF rc,i
  68.     DEF oldgadget:gadget,olddeft,oldtt:PTR TO LONG
  69.     StrCopy(defdrawer,'ENV:Sys')
  70.     iconbase:=OpenLibrary('icon.library',37)
  71.     gettooltypes(name)
  72.     bguibase:=OpenLibrary('bgui.library',40) -> WM_WHICHOBJECT requires V40
  73.     wd_obj:=WindowObject,
  74.         WINDOW_Title,'WReplaceIcon by JT',
  75.         WINDOW_AutoAspect,TRUE,
  76.         WINDOW_Position,POS_CENTERMOUSE,
  77.         WINDOW_ScaleWidth,0,
  78.         WINDOW_ScaleHeight,0,
  79.         WINDOW_SizeGadget,FALSE,
  80.         WINDOW_AppWindow,TRUE,
  81.         WINDOW_CloseOnEsc,TRUE,
  82.         WINDOW_MenuStrip,
  83.             [
  84.              1,0,'Project',NIL,0,0,0,
  85.                 2,0,'About...','A',0,0,ID_ABOUT,
  86.                 2,0,-1,0,0,0,0,
  87.                 2,0,'Select icon...','O',0,0,ID_SELECT,
  88.                 2,0,-1,0,0,0,0,
  89.                 2,0,'Quit','Q',0,0,ID_QUIT,
  90.              1,0,'Options',NIL,0,0,0,
  91.                 2,0,'Copy image','I',CHECKIT OR MENUTOGGLE,0,ID_CIMAGE,
  92.                 2,0,'Copy type and default tool','T',CHECKIT OR MENUTOGGLE,0,ID_CTYPE,
  93.                 2,0,'Copy tool types','L',CHECKIT OR MENUTOGGLE,0,ID_CTTYPES,
  94.             0,0,NIL,0,0,0]:newmenu,
  95.         WINDOW_MasterGroup,
  96.             HGroupObject,Spacing(4),HOffset(4),VOffset(4),GROUP_EqualWidth,doeqw,
  97.                 StartMember,g_Src:=ButtonObject,
  98.                     FRM_Type,FRTYPE_DROPBOX,
  99.                     FRM_Recessed,FALSE,
  100.                     LAB_Label,'_Source',
  101.                     LAB_Place,PLACE_ABOVE,
  102.                     GA_ID,ID_SRC,
  103.                     BUTTON_Image,[0,0,maxw,maxh,0,NIL,0,0,NIL]:image,
  104.                 EndObject,EndMember,
  105.                 StartMember,g_Dest:=InfoObject,
  106.                     FRM_Type,FRTYPE_DROPBOX,
  107.                     FRM_Recessed,FALSE,
  108.                     LAB_Label,'Destination',
  109.                     LAB_Place,PLACE_ABOVE,
  110.                 EndObject,EndMember,
  111.             EndObject,EndMember,
  112.     EndObject
  113.     IF wd_obj=NIL THEN Raise(ERR_GUI)
  114.     IF copy AND CP_IMAGE THEN domethod(wd_obj,[WM_CHECKITEM,ID_CIMAGE,TRUE])
  115.     IF copy AND CP_TYPE THEN domethod(wd_obj,[WM_CHECKITEM,ID_CTYPE,TRUE])
  116.     IF copy AND CP_TTYPES THEN domethod(wd_obj,[WM_CHECKITEM,ID_CTTYPES,TRUE])
  117.     GadgetKey(wd_obj,g_Src,'s')
  118.     wnd:=WindowOpen(wd_obj)
  119.     IF wnd=NIL THEN Raise(ERR_WIN)
  120.     GetAttr(WINDOW_SigMask,wd_obj,{sig})
  121.     GetAttr(WINDOW_AppMask,wd_obj,{i})
  122.     sig:=sig OR i
  123.     done:=FALSE
  124.     WHILE done=FALSE
  125.         Wait(sig)
  126.         WHILE (rc:=HandleEvent(wd_obj))<>WMHI_NOMORE
  127.             SELECT rc
  128.                 CASE WMHI_CLOSEWINDOW; done:=TRUE
  129.                 CASE ID_ABOUT; about()
  130.                 CASE ID_SELECT; selecticon(name)
  131.                 CASE ID_QUIT; done:=TRUE
  132.                 CASE ID_CIMAGE
  133.                     IF domethod(wd_obj,[WM_ITEMCHECKED,rc]) THEN copy:=copy OR CP_IMAGE ELSE copy:=copy AND Not(CP_IMAGE)
  134.                 CASE ID_CTYPE
  135.                     IF domethod(wd_obj,[WM_ITEMCHECKED,rc]) THEN copy:=copy OR CP_TYPE ELSE copy:=copy AND Not(CP_TYPE)
  136.                 CASE ID_CTTYPES
  137.                     IF domethod(wd_obj,[WM_ITEMCHECKED,rc]) THEN copy:=copy OR CP_TTYPES ELSE copy:=copy AND Not(CP_TTYPES)
  138.             ENDSELECT
  139.         ENDWHILE
  140.         WHILE amsg:=GetAppMsg(wd_obj)
  141.             rc:=NIL
  142.             IF amsg.numargs THEN rc:=domethod(wd_obj,[WM_WHICHOBJECT])
  143.             IF rc=g_Src
  144.                 IF diskobj_src THEN FreeDiskObject(diskobj_src)
  145.                 diskobj_src:=NIL
  146.                 wa:=amsg.arglist[0]
  147.                 diskobj_src:=getdiskobj(wa,name)
  148.                 setsource()
  149.             ELSEIF rc=g_Dest
  150.                 IF diskobj_src<>NIL AND copy
  151.                     IF diskobj_dst THEN FreeDiskObject(diskobj_dst)
  152.                      diskobj_dst:=NIL
  153.                     FOR i:=0 TO amsg.numargs-1
  154.                         IF diskobj_dst:=getdiskobj(amsg.arglist[i],name)
  155.                             CopyMem(diskobj_dst.gadget,oldgadget,SIZEOF gadget)
  156.                             IF copy AND CP_IMAGE
  157.                                 CopyMem(diskobj_src.gadget,diskobj_dst.gadget,SIZEOF gadget)
  158.                             ENDIF
  159.                             olddeft:=diskobj_dst.defaulttool
  160.                             IF copy AND CP_TYPE AND (diskobj_dst.type=WBTOOL OR (diskobj_dst.type=WBPROJECT))
  161.                                 diskobj_dst.type:=diskobj_src.type
  162.                                 diskobj_dst.defaulttool:=diskobj_src.defaulttool
  163.                             ENDIF
  164.                             oldtt:=diskobj_dst.tooltypes
  165.                             IF copy AND CP_TTYPES
  166.                                 diskobj_dst.tooltypes:=diskobj_src.tooltypes
  167.                             ENDIF
  168.                             cd:=CurrentDir(amsg.arglist[i].lock)
  169.                             IF PutDiskObject(name,diskobj_dst)=0
  170.                                 -> avoid Skip/Abort choice when it doesn't matter
  171.                                 IF bguireq(NIL,'Unable to write back icon.',
  172.                                     IF i=(amsg.numargs-1) THEN '*_OK' ELSE '*_Skip|_Abort')=
  173.                                     0 THEN i:=amsg.numargs
  174.                             ENDIF
  175.                             CurrentDir(cd)
  176.                             CopyMem(oldgadget,diskobj_dst.gadget,SIZEOF gadget)
  177.                             diskobj_dst.defaulttool:=olddeft
  178.                             diskobj_dst.tooltypes:=oldtt
  179.                             FreeDiskObject(diskobj_dst); diskobj_dst:=NIL
  180.                         ELSE
  181.                             IF bguireq(NIL,'Can''t get icon!',
  182.                                 IF i=(amsg.numargs-1) THEN '*_OK' ELSE '*_Skip|_Abort')=
  183.                                 0 THEN i:=amsg.numargs -> can't use EXIT inside IF
  184.                         ENDIF
  185.                     ENDFOR
  186.                 ELSE
  187.                     DisplayBeep(0)
  188.                 ENDIF
  189.             ELSE
  190.                 DisplayBeep(0)
  191.             ENDIF
  192.             ReplyMsg(amsg)
  193.         ENDWHILE
  194.     ENDWHILE
  195. EXCEPT DO
  196.     IF wd_obj THEN DisposeObject(wd_obj)
  197.     IF filereq THEN DisposeObject(filereq)
  198.     IF bguibase THEN CloseLibrary(bguibase)
  199.     IF diskobj_dst THEN FreeDiskObject(diskobj_dst)
  200.     IF diskobj_src THEN FreeDiskObject(diskobj_src)
  201.     IF iconbase THEN CloseLibrary(iconbase)
  202.     SELECT exception
  203.         CASE ERR_LIB; WriteF('Error: could not open icon or BGUI library.\n')
  204.         CASE ERR_GUI; WriteF('Error: could not create GUI object.\n')
  205.         CASE ERR_WIN; WriteF('Error: could not open window.\n')
  206.     ENDSELECT
  207. ENDPROC
  208.  
  209. PROC getdiskobj(wa:PTR TO wbarg,name:PTR TO CHAR)
  210.     DEF do=NIL:PTR TO diskobject,cd
  211.     StrCopy(name,wa.name)
  212.     IF name[]
  213.         cd:=CurrentDir(wa.lock)
  214.         do:=GetDiskObjectNew(name)
  215.         CurrentDir(cd)
  216.     ELSE -> drawer, garbage or disk
  217.         IF NameFromLock(wa.lock,name,StrMax(name))
  218.             SetStr(name,StrLen(name))
  219.             IF name[EstrLen(name)-1]=":" THEN StrAdd(name,'disk')
  220.             do:=GetDiskObjectNew(name)
  221.         ENDIF
  222.     ENDIF
  223. ENDPROC do
  224.  
  225. PROC gettooltypes(tempstr:PTR TO CHAR /* stratch EString */)
  226.     DEF wb:PTR TO wbstartup,cd
  227.     IF wb:=wbmessage
  228.         cd:=CurrentDir(wb.arglist[0].lock)
  229.         readtt(wb.arglist[0].name)
  230.         CurrentDir(cd)
  231.     ELSE
  232.         GetProgramName(tempstr,StrMax(tempstr))
  233.         cd:=CurrentDir(GetProgramDir())
  234.         readtt(FilePart(tempstr))
  235.         CurrentDir(cd)
  236.     ENDIF
  237. ENDPROC
  238.  
  239. PROC readtt(name:PTR TO CHAR)
  240.     DEF s,do:PTR TO diskobject,tt:PTR TO LONG
  241.     IF (do:=GetDiskObject(name))=NIL THEN RETURN
  242.     tt:=do.tooltypes
  243.     IF s:=FindToolType(tt,'EQUALWIDTH')
  244.         doeqw:=TRUE
  245.         IF s[]="n" OR (s[]="N") THEN doeqw:=FALSE
  246.     ENDIF
  247.     IF s:=FindToolType(tt,'MAXWIDTH')
  248.         maxw:=Max(Val(s),5)
  249.     ENDIF
  250.     IF s:=FindToolType(tt,'MAXHEIGHT')
  251.         maxh:=Max(Val(s),5)
  252.     ENDIF
  253.     IF s:=FindToolType(tt,'COPYIMAGE')
  254.         copy:=copy OR CP_IMAGE
  255.         IF s[]="n" OR (s[]="N") THEN copy:=copy AND Not(CP_IMAGE)
  256.     ENDIF
  257.     IF s:=FindToolType(tt,'COPYTYPE')
  258.         copy:=copy OR CP_TYPE
  259.         IF s[]="n" OR (s[]="N") THEN copy:=copy AND Not(CP_TYPE)
  260.     ENDIF
  261.     IF s:=FindToolType(tt,'COPYTOOLTYPES')
  262.         copy:=copy OR CP_TTYPES
  263.         IF s[]="n" OR (s[]="N") THEN copy:=copy AND Not(CP_TTYPES)
  264.     ENDIF
  265.     IF s:=FindToolType(tt,'DEFDRAWER')
  266.         StrCopy(defdrawer,s)
  267.     ENDIF
  268.     FreeDiskObject(do)
  269. ENDPROC
  270.  
  271. PROC about()
  272.     bguireq('About WReplaceIcon',
  273.         '\ec\s\n'+
  274.         'Freeware\n'+
  275.         'Copyright © 1999 Jilles Tjoelker\n\n'+
  276.         '\ebE-mail:\n\enM.Tjoelker@nl.cis.philips.com\n\n'+
  277.         '\ebSnail mail:\en\nJilles Tjoelker\n'+
  278.         'Craterlaan 6\n'+
  279.         '5632 AG Eindhoven\n'+
  280.         'THE NETHERLANDS','*_OK',[{verstag}+6])
  281. ENDPROC
  282.  
  283. PROC selecticon(name:PTR TO CHAR /* stratch EString */)
  284.     DEF drw,file,path
  285.     IF filereq=NIL
  286.         filereq:=FileReqObject,
  287.             ASLFR_INITIALPATTERN,'#?.info',
  288.             ASLFR_TITLETEXT,'Select icon',
  289.             ASLFR_POSITIVETEXT,'Load',
  290.             ASLFR_INITIALDRAWER,defdrawer,
  291.         EndObject
  292.         IF filereq=NIL THEN RETURN DisplayBeep(NIL)
  293.     ENDIF
  294.     IF DoRequest(filereq)=ASLREQ_OK
  295.         IF diskobj_src THEN FreeDiskObject(diskobj_src)
  296.         diskobj_src:=NIL
  297.         GetAttr(FILEREQ_Drawer,filereq,{drw})
  298.         GetAttr(FILEREQ_File,filereq,{file})
  299.         GetAttr(FILEREQ_Path,filereq,{path})
  300.         IF file[]=0
  301.             StrCopy(name,drw)
  302.             IF name[StrLen(name)-1]=":" THEN StrAdd(name,'disk')
  303.             diskobj_src:=GetDiskObjectNew(name)
  304.         ELSE
  305.             StrCopy(name,path)
  306.             IF diskobj_src=NIL AND (EstrLen(name)>5)
  307.                 IF StrCmp(name+EstrLen(name)-5,'.info')
  308.                     SetStr(name,EstrLen(name)-5) -> strip .info
  309.                 ENDIF
  310.             ENDIF
  311.             diskobj_src:=GetDiskObjectNew(name)
  312.         ENDIF
  313.         setsource()
  314.     ENDIF
  315. ENDPROC
  316.  
  317. PROC setsource()
  318.     DEF img:PTR TO image
  319.     IF diskobj_src
  320.         img:=diskobj_src.gadget.gadgetrender
  321.         IF img.width>maxw OR (img.height>maxh) THEN img:=NIL
  322.         SetGadgetAttrsA(g_Src,wnd,NIL,[BUTTON_Image,img,0])
  323.         IF diskobj_src.gadget.selectrender
  324.             img:=diskobj_src.gadget.selectrender
  325.             IF img.width>maxw OR (img.height>maxh) THEN img:=NIL
  326.             SetGadgetAttrsA(g_Src,wnd,NIL,[BUTTON_SelectedImage,img,0])
  327.         ELSE
  328.             SetGadgetAttrsA(g_Src,wnd,NIL,[BUTTON_SelectedImage,img,0])
  329.         ENDIF
  330.     ELSE
  331.         SetGadgetAttrsA(g_Src,wnd,NIL,[BUTTON_Image,NIL,BUTTON_SelectedImage,NIL,0])
  332.         bguireq(NIL,'Can''t get source icon!','*_OK')
  333.     ENDIF
  334. ENDPROC
  335.